import 'package:core_package/core_package.dart';
import 'package:core_package/share/my_font_weight.dart';
import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/event_or_delegate/reload_event.dart';
import 'package:flutter_app_unionad/model/device_info_model.dart';
import 'package:flutter_app_unionad/model/user_device_list_model.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/request/partner_ship_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:flutter_app_unionad/ui/my_facility/make_an_appointment_install_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/recharge_order_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/recharge_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/my_facility_await_install_widget.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/my_facility_widget.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/recharge_finish_ad_widget.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/select_pay_type_widget.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/share_botton_widget.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item5_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item6_page.dart';
import 'package:flutter_app_unionad/view_model/change_device_event.dart';
import 'package:flutter_unionad/flutter_unionad.dart' as FlutterUnionad;

//我的设备
class MyFacilityPages extends StatefulWidget {
  @override
  _MyFacilityPagesState createState() => _MyFacilityPagesState();
}

//
class _MyFacilityPagesState extends State<MyFacilityPages> with AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
  bool isInstall = false; //是否安装了
  bool appointment = false; //是否预约了安装
  DeviceInfoModel? infoModel;
  UserDeviceListModel? userDeviceListModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    PartnerShipRequest.getConfigBykey(1).then((value) {
      if (value != null && mounted) {
        setState(() {
          Configuration().phone = value;
        });
      }
    });

    eventBus.on<TabViewReloadDataEvent>().listen((event) {
      if (event.index == 0) {
        getUserDeviceModel();
      }
    });
    getUserDeviceModel(needSave: true);
  }

  void getUserDeviceModel({bool needSave = false}) {
    FacilityRequest.getUserDeviceListRequest().then((value) {
      if (value != null) {
        userDeviceListModel = value;
        if (userDeviceListModel!.devList != null && userDeviceListModel!.devList!.length != 0) {
          //有设备
          appointment = false;
          isInstall = true;
          if (UserCacheUil.getString(Configuration.deviceId) == null ||
              UserCacheUil.getString(Configuration.deviceId)!.stringIsEmpty) {
            for (var item in userDeviceListModel!.devList!) {
              if (item.isDefault == 1) {
                UserCacheUil.setString(Configuration.deviceId, item.devId!);
                break;
              }
            }
          }
          if (UserCacheUil.getString(Configuration.deviceId) == null ||
              UserCacheUil.getString(Configuration.deviceId)!.stringIsEmpty) {
            UserCacheUil.setString(Configuration.deviceId, userDeviceListModel!.devList![0].devId!);
          }
          getLoad();
        } else {
          print('没有设备');
          //没有设备
          isInstall = false;
          appointment = userDeviceListModel!.bookingStatus ?? false;
          if (mounted) setState(() {});
        }
      }
    });
  }

  void getLoad() async {
    //获取设备信息
    String? device = UserCacheUil.getString(Configuration.deviceId);
    FacilityRequest.getDevice(device: device).then((value) {
      if (value != null && mounted) {
        infoModel = value;
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildContent(),
    );
  }

  Widget buildContent() {
    if (Configuration().phone.stringIsEmpty || userDeviceListModel == null) {
      return ProgressDialog.showFirstLoading();
    }
    if (isInstall && infoModel == null) {
      return ProgressDialog.showFirstLoading();
    }
    // appointment = true;
    return !isInstall && !appointment
        ? MyFacilityAwaitInstallWidget(
            onTap: () {
              NavigatorUtil.push(
                context,
                UserCenterItem6Page.routeName,
                arguments: false,
              ).then((value) {
                if (value != null && value) {
                  //预约成功
                  getUserDeviceModel();
                }
              });
            },
          )
        : Container(
            width: SizeFill.screenWidth,
            height: SizeFill.screenHeight,
            child: Stack(
              children: [
                MyFacilityWidget(
                  infoModel: infoModel,
                  rechargeOnTop: () {
                    print(infoModel);
                    if (infoModel == null) return;
                    NavigatorUtil.push(context, RechargePage.routeName, arguments: infoModel)
                        .then((value) => getLoad());
                  },
                  rechargeOrderOnTop: () {
                    // RechargeFinishAdWidget.show(
                    //   context,
                    //   orderID: "record ?? ''",
                    // );
                    // return;
                    NavigatorUtil.push(context, RechargeOrderPage.routeName);
                  },
                  isAwait: appointment,
                  userDeviceListModel: userDeviceListModel,
                  reloadOnTop: () {
                    getUserDeviceModel();
                  },
                ),
                // if (appointment)
                //   Positioned(
                //     left: 10.px,
                //     bottom: 10.px,
                //     child: ShareBottomWidget(),
                //   ),
              ],
            ),
          );
  }
}
